package com.hsjk.szwj.module.train.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsjk.szwj.framework.common.pojo.PageResult;
import com.hsjk.szwj.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.hsjk.szwj.framework.security.core.util.SecurityFrameworkUtils;
import com.hsjk.szwj.framework.tenant.core.aop.TenantIgnore;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminCheckoutOrderUpdateReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminMemberCheckoutOrderPageReqVO;
import com.hsjk.szwj.module.train.controller.admin.vo.AdminMemberCheckoutOrderPageRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.checkoutorder.AppMyCheckoutOrderDetailReqVO;
import com.hsjk.szwj.module.train.controller.app.vo.checkoutorder.AppMyCheckoutOrderDetailRespVO;
import com.hsjk.szwj.module.train.controller.app.vo.checkoutorder.AppMyCheckoutOrderSummaryRespVO;
import com.hsjk.szwj.module.train.dal.dataobject.order.CheckoutOrderDO;
import com.hsjk.szwj.module.train.dal.mysql.order.ICheckoutOrderMapper;
import com.hsjk.szwj.module.train.enums.CheckoutOrderStatusEnum;
import com.hsjk.szwj.module.train.service.ICheckoutOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/09/11 17:25
 **/
@Service
public class CheckoutOrderServiceImpl implements ICheckoutOrderService {
    @Resource
    private ICheckoutOrderMapper checkoutOrderMapper;


    @Override
    public PageResult<AppMyCheckoutOrderDetailRespVO> getMyPerformanceDetailPage(AppMyCheckoutOrderDetailReqVO reqVO) {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        reqVO.setMemberId(loginUserId);
        Page<AppMyCheckoutOrderDetailReqVO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        IPage<AppMyCheckoutOrderDetailRespVO> myPerformanceDetailPage = checkoutOrderMapper.getMyPerformanceDetailPage(page, reqVO);
        return new PageResult<>(myPerformanceDetailPage.getRecords(), myPerformanceDetailPage.getTotal());
    }

    @Override
    @TenantIgnore
    public AppMyCheckoutOrderSummaryRespVO getMyCheckoutOrderSummary() {
        AppMyCheckoutOrderSummaryRespVO respVO = null;
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        List<CheckoutOrderDO> checkoutOrderDOS = checkoutOrderMapper.selectList(new LambdaQueryWrapperX<CheckoutOrderDO>()
                .eq(CheckoutOrderDO::getMemberId, loginUserId));
        if (CollUtil.isNotEmpty(checkoutOrderDOS)) {
            respVO = new AppMyCheckoutOrderSummaryRespVO();
            Long hasSettlePrice = 0L;
            Long notSettlePrice = 0L;
            Long totalPrice = 0L;
            for (CheckoutOrderDO orderDO : checkoutOrderDOS) {
                if (CheckoutOrderStatusEnum.SETTLED.getValue().equals(orderDO.getStatus())) {
                    hasSettlePrice += orderDO.getCheckoutAmount();
                } else if (CheckoutOrderStatusEnum.UN_SETTLED.getValue().equals(orderDO.getStatus())) {
                    notSettlePrice += orderDO.getCheckoutAmount();
                }
            }
            totalPrice = hasSettlePrice + notSettlePrice;
            respVO.setHasSettlePrice(hasSettlePrice);
            respVO.setTotalPrice(totalPrice);
            respVO.setNotSettlePrice(notSettlePrice);
        }
        return respVO;
    }

    @Override
    public PageResult<AdminMemberCheckoutOrderPageRespVO> memberCheckoutOrderPage(AdminMemberCheckoutOrderPageReqVO reqVO) {
        Page<AdminMemberCheckoutOrderPageReqVO> page = new Page<>(reqVO.getPageNo(), reqVO.getPageSize());
        IPage<AdminMemberCheckoutOrderPageRespVO> adminMemberCheckoutOrderPageRespVOIPage = checkoutOrderMapper.memberCheckoutOrderPage(page, reqVO);
        return new PageResult<>(adminMemberCheckoutOrderPageRespVOIPage.getRecords(), adminMemberCheckoutOrderPageRespVOIPage.getTotal());
    }

    @Override
    public Boolean checkoutOrderUpdate(AdminCheckoutOrderUpdateReqVO reqVO) {
        CheckoutOrderDO checkoutOrderDO = new CheckoutOrderDO();
        checkoutOrderDO.setId(reqVO.getCheckoutOrderId());
        checkoutOrderDO.setStatus(reqVO.getStatus());
        checkoutOrderMapper.updateById(checkoutOrderDO);
        return Boolean.TRUE;
    }

}
